(function($){
	$.fn.IUpload = function(config){
		var __that = this;
	    config = $.extend({}, config);
		var clickElement = config.clickElement || this;
		var url = config.url;
		if(!url){
			throw "url æªæå®";
		}
		this.onUpload = config.onUpload || function(){};
		this.onFinished = config.onFinished || function(){};
		this.receive = config.receive || function(){};
		var form = $("<form style='display: none;'><input type='file' name='file' "+(config.multiple? "multiple='multiple'": "")+" "+(config.accept? "accept='"+config.accept+"'": "")+"></form>");
		if(config.appendElement){
			this.after(form);
		}
		if(config.params){
			for(var name in config.params){
				var value = config.params[name];
				form.append("<input name='" + name + "' value='"+value+"'/>");
			}
		}
		var input = form.find("input");
		input.on("change", function(){
			upload();
		});
		function selectFile(){
			form.find("input").click();
		}
		function clearFile(){
			form.find("input[type='file']").val("");
		}
		function upload(){
			__that.onUpload();
			form.ajaxSubmit({url: url, type: "post", success: function(data){
				__that.receive(data);
				clearFile();
			}});
			__that.onFinished();
		}
		if(clickElement){
			clickElement.off("click", selectFile).on("click", selectFile);
		}
		this.selectFile = selectFile;
		return this;
	}
})(jQuery, window)

function foolIupload(element){
	element = $(element);
	var iUpload = element.IUpload({url: "manage/fileupload2", multiple: true, accept: "*/*", onUpload: function(){
		this.loading = layer.load();
	}, onFinished: function(){
		layer.close(this.loading);
	}});
	iUpload.receive = function(data){
		if(data.code == 0){
			var atms = data.data;
			addAtms(atms);
		}else {
			sys.msg(data);
		}
	}
	function addAtms(atms){
		if(!atms || !atms.length){
			return;
		}
		var html = '';
		for(var i = 0; i < atms.length; i++){
			var item = atms[i];
			var suffix= item.filename.substring(item.filename.lastIndexOf(".")+1);//ååç¼
			suffix = getFormatClass(suffix);
			
			html += '<tr style="border-bottom: #dadada 1px dashed;">';
				html += '<td style="width: 50px;">';
					html += '<i class="iconsize '+suffix+'"></i>';
				html += '</td>';
				html += '<td>';
					html += '<a href="'+item.url+'" target="_blank" data-suffix="'+suffix+'" data-url="'+item.url+'" data-filename="'+item.filename+'">'+item.filename+'</a>';
				html += '</td>';
				html += '<td style="text-align: right;width: 80px;">';
					html += '<span class="btn btn-xs glyphicon glyphicon-trash floatright" onclick="$(this).parent().parent().remove()" style="background-color:rgba(0,0,0,0); color:#00a2e8;"></span>';
				html += '</td>';
			html += '</tr>';
		}
		$(".file-name-container table").append(html);
	}

	function getFormatClass(format){
		//åæï¼https://blog.csdn.net/qq_33769914/article/details/80021613 
		var formatArr = [
			"doc","docx","dotx","dot","docm","dotm","xps","mht","mhtml","rtf","xml","odt",//ææ¡£
			"pdf",//pdf
			"mp3",//mp3
			"xls","xlsx","xlsb","xlsm","xlst",//è¡¨æ ¼
			"jpg","jpeg","png","gif","bmp","jpeg2000",//å¾ç
			"rar","zip",//åç¼©å
			"mp4","avi","rm","rmvb","wmv","f4v","flv"//å½±é³
		];
		if(typeof format != "undefined"){
			format = $.trim(format).toLowerCase();
			if($.inArray(format, formatArr) == -1){
				return "unknown";
			}else{
				return format;
			}
		}else{
			return "unknown";
		}
	}
}


//è·åä¸ä¼ éä»¶
function foolGetFileList(){
    var fileList = [];
    $(".file-name-container").find("a").each(function(){
        var jqThis = $(this);
        fileList.push({
            filename: jqThis.data("filename"),
            url: jqThis.data("url")
        })
    })
    return fileList;
}